package com.anti.modular.bus.busbasicsettings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.commom.api.Result;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.busbasicsettings.entity.BusBasicSettings;
import com.anti.modular.bus.busbasicsettings.enums.BusBasicSettingsExceptionEnum;
import com.anti.modular.bus.busbasicsettings.param.BusBasicSettingsParam;
import com.anti.modular.bus.busbasicsettings.service.BusBasicSettingsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 基础設置表service接口实现类
 *
 * @author anti
 * @date 2021-04-09 10:20:17
 */
@Service
public class BusBasicSettingsServiceImpl implements BusBasicSettingsService {
    @Resource
    DBUtils dbUtils;

    @Override
    public BusBasicSettings getById(BusBasicSettingsParam busBasicSettingsParam){
        String sql="select * from bus_basic_settings where id =? ";
        return dbUtils.get(sql,BusBasicSettings.class,busBasicSettingsParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,BusBasicSettingsParam busBasicSettingsParam) {
        StringBuilder sb=new StringBuilder("select * from bus_basic_settings where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(busBasicSettingsParam)) {
            // 根据计算费用时月天数 查询
            if (ObjectUtil.isNotEmpty(busBasicSettingsParam.getCalculateStyle())) {
               sb.append(" and calculate_style = ?");
               param.add(busBasicSettingsParam.getCalculateStyle());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public BusBasicSettings getOne() {
        String sql="select * from bus_basic_settings where status =0 ";
        return dbUtils.getFirst(sql,BusBasicSettings.class);
    }

    @Override
    public List<BusBasicSettings> list(BusBasicSettingsParam busBasicSettingsParam) {
        String sql="";
        return dbUtils.query(sql,BusBasicSettings.class);
    }

    @Override
    public void add(BusBasicSettingsParam busBasicSettingsParam) {
        BusBasicSettings busBasicSettings = new BusBasicSettings();
        BeanUtil.copyProperties(busBasicSettingsParam, busBasicSettings);
        busBasicSettings.setStatus(0);
        busBasicSettings.setCreateTime(new Date());
        busBasicSettings.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(busBasicSettings);
    }


    public void delete(BusBasicSettingsParam busBasicSettingsParam) {
        String sql="delete  from bus_basic_settings where id =? ";
        dbUtils.execSql(sql,busBasicSettingsParam.getId());
    }

    public void edit(BusBasicSettingsParam busBasicSettingsParam) {
        if (ObjectUtil.isEmpty(busBasicSettingsParam.getId())){
            this.add(busBasicSettingsParam);
        }else {
            BusBasicSettings busBasicSettings = this.queryBusBasicSettings(busBasicSettingsParam);
            BeanUtil.copyProperties(busBasicSettingsParam, busBasicSettings);
            busBasicSettings.setUpdateTime(new Date());
            busBasicSettings.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
            dbUtils.update(busBasicSettings);
        }
    }

    @Override
    public BusBasicSettings detail(BusBasicSettingsParam busBasicSettingsParam) {
        return this.queryBusBasicSettings(busBasicSettingsParam);
    }

    /**
     * 获取基础設置表
     *
     * @author anti
     * @date 2021-04-09 10:20:17
     */
    private BusBasicSettings queryBusBasicSettings(BusBasicSettingsParam busBasicSettingsParam) {
        BusBasicSettings busBasicSettings = this.getById(busBasicSettingsParam);
        if (ObjectUtil.isNull(busBasicSettings)) {
            throw new BusinessException(BusBasicSettingsExceptionEnum.NOT_EXIST);
        }
        return busBasicSettings;
    }


}
